package com.taldh.design.patterns.proxy.dynamic;

import java.lang.reflect.Method;

 

import net.sf.cglib.proxy.Callback;

import net.sf.cglib.proxy.CallbackFilter;

import net.sf.cglib.proxy.Enhancer;

import net.sf.cglib.proxy.MethodInterceptor;

import net.sf.cglib.proxy.MethodProxy;

import net.sf.cglib.proxy.NoOp;

 

public class CallbackFilterDemo {

    public static void main(String[] args) {

        // 回调实例数组

        Callback[] callbacks = new Callback[]{new MethodInterceptorImpl(), NoOp.INSTANCE};


        // 使用enhancer，设置相关参数。

        Enhancer enhancer = new Enhancer();

        enhancer.setSuperclass(User.class);

        enhancer.setCallbacks(callbacks);

        enhancer.setCallbackFilter(new CallbackFilterImpl());


        // 产生代理对象

        User proxyUser = (User) enhancer.create();


        proxyUser.pay(); // 买

        proxyUser.eat(); // 吃

    }
}